//
// Created by A on 2024/6/22.
//
#include "config.h"

//初始化SLD资源
int Game_Init(){
    if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0)
    {
        SDL_Log("SDL_Init failed: %s", SDL_GetError());
        Game_DeInit();
        return -1;
    }

    IMG_Init(IMG_INIT_PNG);
    TTF_Init();

    font = TTF_OpenFont(FONTPath,20);
    if(!font){
        printf("font load final\n");
        return -1;
    }
    //创建窗口
    window = SDL_CreateWindow(
            "Helloworld",
            SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
            WIDTH, HEIGHT,
            SDL_WINDOW_SHOWN);
    if (nullptr == window)
    {
        SDL_Log("SDL_CreateWindow failed: %s", SDL_GetError());
        Game_DeInit();
        return -1;
    }
    //创建渲染器
    ren = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);// | SDL_RENDERER_PRESENTVSYNC
    if (ren == nullptr){
        Game_DeInit();
        return -1;
    }

    //初始化精灵链表
    Spirit_Link = List_init();
    if(Spirit_Link == NULL){//初始化失败
        Game_DeInit();
        return -1;
    }


    /**===========资源加载================*/
//    Mspirit * Mlead[22][13];
//    for (int j = 0; j < 13 ;j++) {
//        for (int i = 0; i < 22; i++) {
//            Mlead[i][j]=new Mspirit; //主角
//            lead_Init(Mlead[i][j]);
//            Mlead[i][j]->Rect.x=50+i*50;
//            Mlead[i][j]->Rect.y=50+j*50;
//            Mspirit_joinLink(Mlead[i][j],Spirit_Link);
//
//        }
//    }
    Mspirit * ptr =new Mspirit; //主角
    lead_Init(ptr);
    Mspirit_joinLink(ptr,Spirit_Link);

    /**===========图片加载================*/
    SDL_Surface *bmp = IMG_Load(Mapfile.c_str());
    if (bmp == nullptr){
        Game_DeInit();
        return 1;
    }

    Mapfiletex = SDL_CreateTextureFromSurface(ren, bmp);

    SDL_FreeSurface(bmp);
    if (Mapfiletex == nullptr){
        Game_DeInit();
        return 1;
    }

    /**===========初始化资源载入===========*/
    SDL_Rect ss1={
            .x =0,
            .y =0,
            .w =24,
            .h =24
    };
    SDL_RenderClear(ren);
    //Draw the texture
    for (int i = 0; i < HEIGHT/grass_road.h; ++i) {
        for (int j = 0; j < WIDTH/ss1.w; ++j) {
            SDL_RenderCopyEx(ren, Mapfiletex, &grass_road, &ss1,0,NULL,SDL_FLIP_NONE);
            //printf("x =%d \n",ss1.x);
            ss1.x+=ss1.w;
        }
        ss1.y+=ss1.h;
        ss1.x=0;
       // printf("y =%d \n",ss1.y);
    }
    ss1.x=200;
    ss1.y=153;
    ss1.h=72;
    ss1.w=72;
    SDL_RenderCopyEx(ren, Mapfiletex, &grass, &ss1,0,NULL,SDL_FLIP_NONE);
    //Update the screen
    SDL_RenderPresent(ren);



    return 0;

}
//卸载释放SLD资源
int Game_DeInit(){//考虑资源是否可以通过链表来存储和释放
    if(window!=nullptr)
      SDL_DestroyWindow(window);

    if(ren!=nullptr)
      SDL_DestroyRenderer(ren);

    if(Mapfiletex!=nullptr)
     SDL_DestroyTexture(Mapfiletex);

    if(font!=nullptr)
        TTF_CloseFont(font);

    IMG_Quit();
    SDL_Quit();

    return 0;
}


//为精灵绑定资源
void lead_Init(Mspirit *lead){

    //基础属性 Attribute
    lead->attribute.key =leader;

    //精灵状态 Allstatus
    lead->Status=idle;

    //当前位置大小 Spirit_Rect
    lead->Rect.w=34;
    lead->Rect.h=48;
    lead->Rect.x=28;
    lead->Rect.y=35;

    //武器装备 Weapon


    //动画 AllAnimation

    //动画资源加载
    SDL_Surface *   bmp = IMG_Load(Leadfile.c_str());
    if (bmp == nullptr){
        return ;
    }
    SDL_Texture * resoures = SDL_CreateTextureFromSurface(ren, bmp);
    SDL_FreeSurface(bmp);
//CH0010B
//    //动画状态帧加载---空闲状态
//    lead->mation.idle.type=6;
//    lead->mation.idle.frame_num[lead->mation.idle.type] = 5;
//    lead->mation.idle.framedata[lead->mation.idle.type] =(Animation_frame *)malloc(sizeof(Animation_frame)*lead->mation.idle.frame_num[lead->mation.idle.type]);//初始5帧
//    for (int i = 0; i < lead->mation.idle.frame_num[lead->mation.idle.type]; ++i) {
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.x=i*128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.y=0;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.w=128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.h=128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].delat_time=200;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].resoures=resoures;
//    }
//
//    lead->mation.idle.type=7;
//    lead->mation.idle.frame_num[lead->mation.idle.type] = 5;
//    lead->mation.idle.framedata[lead->mation.idle.type] =(Animation_frame *)malloc(sizeof(Animation_frame)*lead->mation.idle.frame_num[lead->mation.idle.type]);//初始5帧
//    for (int i = 0; i < lead->mation.idle.frame_num[lead->mation.idle.type]; ++i) {
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.x=i*128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.y=1*128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.w=128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.h=128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].delat_time=200;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].resoures=resoures;
//    }
//
//    lead->mation.idle.type=0;
//    lead->mation.idle.frame_num[lead->mation.idle.type] = 5;
//    lead->mation.idle.framedata[lead->mation.idle.type] =(Animation_frame *)malloc(sizeof(Animation_frame)*lead->mation.idle.frame_num[lead->mation.idle.type]);//初始5帧
//    for (int i = 0; i < lead->mation.idle.frame_num[lead->mation.idle.type]; ++i) {
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.x=i*90;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.y=2*128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.w=128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.h=128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].delat_time=200;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].resoures=resoures;
//    }
//
//    lead->mation.idle.type=1;
//    lead->mation.idle.frame_num[lead->mation.idle.type] = 5;
//    lead->mation.idle.framedata[lead->mation.idle.type] =(Animation_frame *)malloc(sizeof(Animation_frame)*lead->mation.idle.frame_num[lead->mation.idle.type]);//初始5帧
//    for (int i = 0; i < lead->mation.idle.frame_num[lead->mation.idle.type]; ++i) {
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.x=i*90;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.y=3*128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.w=128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.h=128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].delat_time=200;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].resoures=resoures;
//    }
//
//    lead->mation.idle.type=2;
//    lead->mation.idle.frame_num[lead->mation.idle.type] = 5;
//    lead->mation.idle.framedata[lead->mation.idle.type] =(Animation_frame *)malloc(sizeof(Animation_frame)*lead->mation.idle.frame_num[lead->mation.idle.type]);//初始5帧
//    for (int i = 0; i < lead->mation.idle.frame_num[lead->mation.idle.type]; ++i) {
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.x=i*90;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.y=4*128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.w=128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.h=128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].delat_time=200;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].resoures=resoures;
//    }
//
//    lead->mation.idle.type=3;
//    lead->mation.idle.frame_num[lead->mation.idle.type] = 5;
//    lead->mation.idle.framedata[lead->mation.idle.type] =(Animation_frame *)malloc(sizeof(Animation_frame)*lead->mation.idle.frame_num[lead->mation.idle.type]);//初始5帧
//    for (int i = 0; i < lead->mation.idle.frame_num[lead->mation.idle.type]; ++i) {
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.x=i*90;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.y=5*128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.w=128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.h=128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].delat_time=200;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].resoures=resoures;
//    }
//
//    lead->mation.idle.type=4;
//    lead->mation.idle.frame_num[lead->mation.idle.type] = 5;
//    lead->mation.idle.framedata[lead->mation.idle.type] =(Animation_frame *)malloc(sizeof(Animation_frame)*lead->mation.idle.frame_num[lead->mation.idle.type]);//初始5帧
//    for (int i = 0; i < lead->mation.idle.frame_num[lead->mation.idle.type]; ++i) {
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.x=i*90;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.y=6*128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.w=128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.h=128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].delat_time=200;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].resoures=resoures;
//    }
//
//    lead->mation.idle.type=5;
//    lead->mation.idle.frame_num[lead->mation.idle.type] = 5;
//    lead->mation.idle.framedata[lead->mation.idle.type] =(Animation_frame *)malloc(sizeof(Animation_frame)*lead->mation.idle.frame_num[lead->mation.idle.type]);//初始5帧
//    for (int i = 0; i < lead->mation.idle.frame_num[lead->mation.idle.type]; ++i) {
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.x=i*90;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.y=7*128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.w=128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.h=128;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].delat_time=200;
//        lead->mation.idle.framedata[lead->mation.idle.type][i].resoures=resoures;
//    }
//
//    lead->mation.idle.type=6;


//204
    //动画状态帧加载---空闲状态
    lead->mation.idle.type=4;
    lead->mation.idle.frame_num[lead->mation.idle.type] = 4;
    lead->mation.idle.framedata[lead->mation.idle.type] =(Animation_frame *)malloc(sizeof(Animation_frame)*lead->mation.idle.frame_num[lead->mation.idle.type]);
    for (int i = 0; i < lead->mation.idle.frame_num[lead->mation.idle.type]; ++i) {
        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.x=i*34;
        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.y=0;
        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.w=34;
        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.h=48;
        lead->mation.idle.framedata[lead->mation.idle.type][i].delat_time=300;
        lead->mation.idle.framedata[lead->mation.idle.type][i].resoures=resoures;
    }
    lead->mation.idle.type=6;
    lead->mation.idle.frame_num[lead->mation.idle.type] = 4;
    lead->mation.idle.framedata[lead->mation.idle.type] =(Animation_frame *)malloc(sizeof(Animation_frame)*lead->mation.idle.frame_num[lead->mation.idle.type]);
    for (int i = 0; i < lead->mation.idle.frame_num[lead->mation.idle.type]; ++i) {
        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.x=i*34;
        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.y=48;
        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.w=34;
        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.h=48;
        lead->mation.idle.framedata[lead->mation.idle.type][i].delat_time=300;
        lead->mation.idle.framedata[lead->mation.idle.type][i].resoures=resoures;
    }
    lead->mation.idle.type=2;
    lead->mation.idle.frame_num[lead->mation.idle.type] = 4;
    lead->mation.idle.framedata[lead->mation.idle.type] =(Animation_frame *)malloc(sizeof(Animation_frame)*lead->mation.idle.frame_num[lead->mation.idle.type]);
    for (int i = 0; i < lead->mation.idle.frame_num[lead->mation.idle.type]; ++i) {
        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.x=i*34;
        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.y=48*2;
        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.w=34;
        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.h=48;
        lead->mation.idle.framedata[lead->mation.idle.type][i].delat_time=300;
        lead->mation.idle.framedata[lead->mation.idle.type][i].resoures=resoures;
    }
    lead->mation.idle.type=0;
    lead->mation.idle.frame_num[lead->mation.idle.type] = 4;
    lead->mation.idle.framedata[lead->mation.idle.type] =(Animation_frame *)malloc(sizeof(Animation_frame)*lead->mation.idle.frame_num[lead->mation.idle.type]);
    for (int i = 0; i < lead->mation.idle.frame_num[lead->mation.idle.type]; ++i) {
        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.x=i*34;
        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.y=48*3;
        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.w=34;
        lead->mation.idle.framedata[lead->mation.idle.type][i].original_Rect.h=48;
        lead->mation.idle.framedata[lead->mation.idle.type][i].delat_time=300;
        lead->mation.idle.framedata[lead->mation.idle.type][i].resoures=resoures;
    }
    lead->mation.idle.type=0;
    //动画状态帧加载---运动状态

    //动画状态帧加载---交互状态

    //动画状态帧加载---出现状态

    //动画状态帧加载---死亡状态

    //动画状态帧加载---攻击状态

}

void PPS_SHOW(void){
    //============FPS================

    if(ENABLE_FPS==0)
        return;
    SDL_Surface * fps_show = TTF_RenderText_Solid(font,fps_num,{0,255,0});
    SDL_Texture * fps_str = SDL_CreateTextureFromSurface(ren,fps_show);
    SDL_Rect  fps_pos ={.x=WIDTH-fps_show->w,.y=0,.w=fps_show->w,.h=fps_show->h};
    SDL_FreeSurface(fps_show);
    SDL_RenderCopyEx(ren, fps_str, NULL, &fps_pos,0,NULL,SDL_FLIP_NONE);
    //=============================

}